package com.study.shengsiyuan.example08;
import java.util.UUID;

import com.study.shengsiyuan.example08.proto.MyRequest;
import com.study.shengsiyuan.example08.proto.MyResponse;
import com.study.shengsiyuan.example08.proto.StreamRequest;
import com.study.shengsiyuan.example08.proto.StreamResponse;
import com.study.shengsiyuan.example08.proto.StudentRequest;
import com.study.shengsiyuan.example08.proto.StudentResponse;
import com.study.shengsiyuan.example08.proto.StudentResponseList;
import com.study.shengsiyuan.example08.proto.StudentServiceGrpc;


import io.grpc.stub.StreamObserver;
public class StudentServiceIpml extends StudentServiceGrpc.StudentServiceImplBase {

	@Override
	public void getRealnameByUsername(MyRequest request, StreamObserver<MyResponse> responseObserver) {
		//super.getRealnameByUsername(request, responseObserver);
		//接收客户端数据
		System.out.println("接受到客户端信息 ："+request.getUsername());
		//向客户端写数据
		responseObserver.onNext(
					MyResponse.newBuilder().setRealname("张三") .build() );

		responseObserver.onCompleted();
	}

	@Override
	public void getStudentsByAge(StudentRequest request, StreamObserver<StudentResponse> responseObserver) {
		//super.getStudentsByAge(request, responseObserver);
		System.out.println("接受到客户端信息 ："+request.getAge());
		responseObserver
			.onNext( StudentResponse.newBuilder().setName("张三").setAge(12).setCity("合肥市").build());
		responseObserver
			.onNext( StudentResponse.newBuilder().setName("张三2").setAge(13).setCity("合肥市").build());
		responseObserver
			.onNext( StudentResponse.newBuilder().setName("张三3").setAge(14).setCity("合肥市").build());
		responseObserver
			.onNext( StudentResponse.newBuilder().setName("张三4").setAge(15).setCity("合肥市").build());

		responseObserver.onCompleted();

	}

	@Override
	public StreamObserver<StudentRequest> getStudentsWrapperByAges(
			StreamObserver<StudentResponseList> responseObserver) {
		return new StreamObserver<StudentRequest>() {

			@Override
			public void onNext(StudentRequest value) {//获取每一次请求的参数
				System.out.println("onNext ：" + value.getAge());

			}

			@Override
			public void onError(Throwable t) {//异常处理
				System.out.println(t.getMessage());

			}

			@Override
			public void onCompleted() {//向客户端返回结果
				StudentResponse sr1= StudentResponse.newBuilder().setName("张三").setAge(12).setCity("合肥市").build();
				StudentResponse sr2= StudentResponse.newBuilder().setName("李四").setAge(12).setCity("蚌埠市").build();
				StudentResponseList list = StudentResponseList.newBuilder().addStudentResponse(sr2).addStudentResponse(sr2).build();
				responseObserver.onNext(list);
				responseObserver.onCompleted();
			}

		};
	}


	@Override
	public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamResponse> responseObserver) {
		return new StreamObserver<StreamRequest>() {

			@Override
			public void onNext(StreamRequest value) {//获取每一次请求的参数
				System.out.println("onNext ：" + value.getRequestInfo());
				responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build());
			}

			@Override
			public void onError(Throwable t) {//异常处理
				System.out.println(t.getMessage());

			}

			@Override
			public void onCompleted() {//向客户端返回结果
				responseObserver.onCompleted();
			}
		};

	}


}
